
#include "serial.h"
#include <usart.h>
#include <task.h>

static TaskHandle_t serialTaskHandle;

#define RX_BUFFER_SIZE (100)

static uint8_t rxBuffer[RX_BUFFER_SIZE];

void serial_Event_handler(void *pvParameters)
{
    while (1)
    {
        HAL_Delay(1000);

        // if (HAL_DMA_GetState(huart1.hdmarx) == HAL_DMA_STATE_READY)
        {
            printf("%s\r\n", rxBuffer);
        }
    }
}

void serial_init(void)
{
    if (xTaskCreate(serial_Event_handler, "Serial", 128, NULL, 5, &serialTaskHandle) != pdPASS)
    {
        printf("Error creating serial task\n");
        return;
    }

    HAL_UART_Receive_DMA(&huart1, (uint8_t *)rxBuffer, RX_BUFFER_SIZE);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart == &huart1)
    {
        // printf("Data: %s\n", rxBuffer);

        HAL_UART_Receive_DMA(&huart1, (uint8_t *)rxBuffer, RX_BUFFER_SIZE);
    }
}
